/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package tetris_invader.windows;

import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Image;
import java.awt.Toolkit;
import java.net.URL;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import tetris_invader.personagens.Fabrica;
import tetris_invader.util.Timer;
import tetris_invader.util.TimerListener;

public class InfoPanel extends JPanel implements TimerListener {

    private transient Timer timer;
    private int points;
    private float energy;
    private int lifes;
    private int level;
    private Image[] pointimg;
    private Image[] scoreimg;
    private Image[] levelImage;
    private Image[] lifeImage;
    private Image[] energyImg;
    private JProgressBar energybar;
    private JPanel barPanel;
    private JPanel drawPanel;

    public InfoPanel() {
        points = 0;
        energy = 15;
        lifes = 0;
        timer = new Timer(this, 10);
        pointimg = loadImage();
        scoreimg = new Image[1];
        levelImage = new Image[1];
        lifeImage = new Image[1];
        energyImg = new Image[1];
        barPanel = new JPanel();
        drawPanel = new JPanel();
        energybar = new JProgressBar();
        energybar.setMaximum(100);
        energybar.setMinimum(0);
        energybar.setValue(15);
        energybar.setLocation(10, 315);
        energybar.setBorderPainted(true);
        barPanel.add(energybar);

        this.setLayout(new GridBagLayout());
        this.add(barPanel, grid(1,15));

        for (int i = 0; i < scoreimg.length; i++) {
            String filename = "/images/number/score.png";
            URL url = Fabrica.class.getResource(filename);
            scoreimg[i] = Toolkit.getDefaultToolkit().getImage(url);
            scoreimg[i] = scoreimg[i].getScaledInstance(180, 50, 1);
        }

        for (int i = 0; i < levelImage.length; i++) {
            String filename = "/images/number/level.png";
            URL url = Fabrica.class.getResource(filename);
            levelImage[i] = Toolkit.getDefaultToolkit().getImage(url);
            levelImage[i] = levelImage[i].getScaledInstance(180, 50, 1);
        }
        
        for (int i = 0; i < lifeImage.length; i++) {
            String filename = "/images/number/life.png";
            URL url = Fabrica.class.getResource(filename);
            lifeImage[i] = Toolkit.getDefaultToolkit().getImage(url);
            lifeImage[i] = lifeImage[i].getScaledInstance(180, 50, 1);
        }

         for (int i = 0; i < energyImg.length; i++) {
            String filename = "/images/number/Energy.png";
            URL url = Fabrica.class.getResource(filename);
            energyImg[i] = Toolkit.getDefaultToolkit().getImage(url);
            energyImg[i] = energyImg[i].getScaledInstance(180, 50, 1);
        }
    }

    public GridBagConstraints grid(int gridx, int gridy){
        GridBagConstraints g = new GridBagConstraints();

        g.gridx = gridx;
        g.gridy = gridy;
        g.gridwidth = 2;
        g.gridheight = 2;
        g.anchor = GridBagConstraints.EAST;
        g.fill = GridBagConstraints.HORIZONTAL;

        return g;
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        Graphics2D g2 = (Graphics2D) g;

        g2.drawImage(scoreimg[0], 55, 10, this);
        g2.drawImage(pointimg[showNumber(0, points)], 235, 65, this);
        g2.drawImage(pointimg[showNumber(1, points)], 190, 65, this);
        g2.drawImage(pointimg[showNumber(2, points)], 145, 65, this);
        g2.drawImage(pointimg[showNumber(3, points)], 100, 65, this);

        g2.drawImage(levelImage[0], 55, 115, this);
        g2.drawImage(pointimg[showNumber(0, level)], 235, 165, this);
        g2.drawImage(pointimg[showNumber(1, level)], 190, 165, this);

        g2.drawImage(lifeImage[0], 55, 285, this);
        g2.drawImage(pointimg[showNumber(0, lifes)], 235, 335, this);
        g2.drawImage(pointimg[showNumber(1, lifes)], 190, 335, this);

        g2.drawImage(energyImg[0], 55, 210, this);
    }

    public void update() {
        this.repaint();
    }

    private int showNumber(int i, int k) {
        int aux = k;

        aux = (int) (aux / Math.pow(10, i));
        return aux % 10;

    }

    public Image[] loadImage() {
        Image img[] = new Image[11];
        for (int i = 0; i < img.length - 1; i++) {
            String filename = "/images/number/number" + i + ".png";
            URL url = Fabrica.class.getResource(filename);
            img[i] = Toolkit.getDefaultToolkit().getImage(url);
            img[i] = img[i].getScaledInstance(45, 45, 1);
        }
        String filename = "/images/number/random.gif";
        URL url = Fabrica.class.getResource(filename);
        img[10] = Toolkit.getDefaultToolkit().getImage(url);
        img[10] = img[10].getScaledInstance(45, 45, 1);

        return img;
    }

    public float getEnergy() {
        return energy;
    }

    public void setEnergy(float energy) {
        this.energy = energy;
        energybar.setValue((int) energy);
    }

    public void addEnergy(float energy) {
        this.energy += energy;
        energybar.setValue((int) energy);
    }

    public int getLifes() {
        return lifes;
    }

    public void setLifes(int lifes) {
        this.lifes = lifes;
    }

    public void addLifes(int lifes) {
        this.lifes += lifes;
    }

    public int getPoints() {
        return points;
    }

    public void setPoints(int points) {
        this.points = points;
    }

    public void addPoints(int points) {
        this.points += points;
    }

    public int getLevel() {
        return level;
    }

    public void setLevel(int level) {
        this.level = level;
    }

    public void addLevel(int level) {
        this.level += level;
    }

    public void start() {
        if (!timer.isAlive()) {
            timer.start();
        } else
            restart();
    }

    public void restart(){
        points = 0;
        lifes = 3;
        energy = 0;
    }
}
